<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
		<title>数据批量替换</title>
		<link rel="stylesheet" href="/static/web/css/font-awesome.min.css">
		<link rel="stylesheet" href="/static/web/css/bootstrap.min.css">
		<link rel="stylesheet" href="/static/web/css/admin.css">
		<script src="/static/web/js/jquery.min.js"></script>
		<script src="/static/web/js/bootstrap.min.js"></script>
		<script src="/static/web/js/webajax.js"></script>
		<script src="/static/web/js/admin.main.js"></script>
	</head>
	<body>
		<div class="container-fluid">
			<ol class="breadcrumb">
				<li class="breadcrumb-item"><a href="index_body.php">后台面板</a></li>
				<li class="breadcrumb-item active">数据批量替换</li>
			</ol>
			<div class="card shadow-sm">
				<div class="card-header">数据批量替换</div>
				<div class="card-body">
					<div class="alert alert-warning">数据批量替换，请谨慎操作</div>
					<form action="sys_data_replace.php" name="form1" method="post" target="stafrm">
						<input type="hidden" name="action" value="apply">
						<div class="table-responsive">
							<table class="table table-borderless">
								<tbody>
									<tr>
										<td width="260">数据表与字段</td>
										<td>
											<?php
											$dsql->SetQuery("Show Tables");
											$dsql->Execute('t');
											if ($dsql->GetError()!=''){
												echo "找不到您所指定的数据库".$dbname."";
												echo $qbutton;
											}
											echo "<select name='exptable' multiple='yes' onchange='ShowFields();' id='exptable' class='admin-input-lg mb-3'>";
											while($row = $dsql->GetArray('t',MYSQL_BOTH)){
												echo "<option value='{$row[0]}'>{$row[0]}</option>";
											}
											echo "</select>";
											$dsql->Close();
											?>
											<div id="fields"></div>
											<input type="text" name="rpfield" id="rpfield" class="admin-input-sm" required>
										</td>
									</tr>
									<tr>
										<td>主键字段</td>
										<td>
											<input type="text" name="keyfield" id="keyfield" class="admin-input-sm">
											<label><input type="radio" name="rptype" id="ot1" value="replace" checked> 普通替换</label>
											<label><input type="radio" name="rptype" id="ot2" value="regex"> 正则表达式</label>
										</td>
									</tr>
									<tr>
										<td>被替换值</td>
										<td><textarea name="rpstring" id="rpstring" class="admin-textarea-xl" required></textarea></td>
									</tr>
									<tr>
										<td>值替换为</td>
										<td><textarea name="tostring" id="tostring" class="admin-textarea-xl"></textarea></td>
									</tr>
									<tr>
										<td>替换条件</td>
										<td><input type="text" name="condition" id="condition" class="admin-input-sm">（不填写则完全替换）</td>
									</tr>
									<tr>
										<td>验证码</td>
										<td>
											<input type="text" name="validate" class="admin-input-sm text-uppercase">
											<img src="<?php echo $cfg_phpurl;?>/vdimgck.php" onclick="this.src='<?php echo $cfg_phpurl;?>/vdimgck.php?'+new Date().getTime()+Math.round(Math.random() * 10000)" title="验证码">
										</td>
									</tr>
									<tr>
										<td colspan="2" align="center"><button type="submit"class="btn btn-success btn-sm">开始替换数据</button></td>
									</tr>
									<tr>
										<td colspan="2">
											<div class="admin-win-iframe"><iframe name="stafrm" frameborder="0" id="stafrm" width="100%" height="100%"></iframe></div>
										</td>
									</tr>
								</tbody>
							</table>
						</div>
					</form>
				</div>
			</div>
		</div>
		<script>
			function ShowFields() {
				var exptable = $DE('exptable').options[$DE('exptable').selectedIndex].value;
				var queryUrl = "sys_data_replace.php?exptable=" + exptable + "&action=getfields";
				fetch(queryUrl).then(resp => {
					if (resp.ok) {
						return resp.text()
					}
					throw new Error('x');
				}).then((d) => {
					$DE('fields').innerHTML = d;
				}).catch((error) => {
					$DE('fields').innerHTML = errMsg;
				});
			}
			function CheckSubmit() {
				if ($DE('rpfield').value == "") {
					ShowMsg("您选择的操作为手工指定字段，但您并没指定");
					return false;
				}
				if ($DE('rpstring').value == "") {
					ShowMsg("您没指定要替换的字符串");
					return false;
				}
				return true;
			}
			function pf(v) {
				$DE('rpfield').value = v;
			}
		</script>
	</body>
</html>